public class Maze {
  private boolean[][] maze= {
      {false, false, false, false, false, false, false, false},
      {false, true,  true,  true,  true,  true,  true,  false},
      {false, true,  false, true,  false, false, false, false},
      {false, false, false, true,  true,  true,  true,  false},
      {false, true,  false, false, false, false, true,  false},
      {false, true,  false, false, true,  false, true,  false},
      {false, true,  true,  true,  true,  false, true,  false},
      {false, false, false, false, false, false, false, false}
    }; 
  private Tuple end;
  
  Maze(){
    
    end = new Tuple(6,6);
  }

  public void walk(Tuple start){
      // bad solution
      boolean[][] isDiscovered = {
      {false, false, false, false, false, false, false, false},
      {false, false, false, false, false, false, false, false},
      {false, false, false, false, false, false, false, false},
      {false, false, false, false, false, false, false, false},
      {false, false, false, false, false, false, false, false},
      {false, false, false, false, false, false, false, false},
      {false, false, false, false, false, false, false, false},
      {false, false, false, false, false, false, false, false}
    };

    // TupleStack stack = new TupleStack();
    Stack<Tuple> stack = new Stack<Tuple>();
    try {
      stack.push(start);
    } catch(Exception e){
      e.printStackTrace();
    }
    
    isDiscovered[start.first][start.second] = true;

    while(!stack.isEmpty()){
      Tuple currentLocation = null;
      try {
        currentLocation = stack.pop();
        if(currentLocation != null){
          System.out.println(currentLocation.toString());
        }
      } catch(Exception e){
        e.printStackTrace();
      }      

      // Termination case
      if(currentLocation !=null && currentLocation.first == end.first && currentLocation.second == end.second){
        // System.out.println(currentLocation.toString());
        System.out.println("Reached");
        break;
      }

      // North
      if(currentLocation !=null && currentLocation.first>0){
        if(maze[currentLocation.first-1][currentLocation.second] && !isDiscovered[currentLocation.first-1][currentLocation.second]){
          Tuple temp = new Tuple(currentLocation.first-1, currentLocation.second);
          try {
              stack.push(temp);
            } catch(Exception e){
              e.printStackTrace();
          }
          isDiscovered[currentLocation.first-1][currentLocation.second] = true;
        }
      }
      // West
      if(currentLocation !=null && currentLocation.second>0){
        if(maze[currentLocation.first][currentLocation.second-1] && !isDiscovered[currentLocation.first][currentLocation.second-1]){
          Tuple temp = new Tuple(currentLocation.first, currentLocation.second-1);
          try {
              stack.push(temp);
            } catch(Exception e){
              e.printStackTrace();
          }
          isDiscovered[currentLocation.first][currentLocation.second-1] = true;
        }
      }
      // South
      if(currentLocation !=null && currentLocation.first<6){
        if(maze[currentLocation.first+1][currentLocation.second] && !isDiscovered[currentLocation.first+1][currentLocation.second]){
          Tuple temp = new Tuple(currentLocation.first+1, currentLocation.second);
          try {
              stack.push(temp);
            } catch(Exception e){
              e.printStackTrace();
          }
          isDiscovered[currentLocation.first+1][currentLocation.second] = true;
        }
      }
      // East
      if(currentLocation !=null && currentLocation.second<6){
        if(maze[currentLocation.first][currentLocation.second+1] && !isDiscovered[currentLocation.first][currentLocation.second+1]){
          Tuple temp = new Tuple(currentLocation.first, currentLocation.second+1);
          try {
              stack.push(temp);
            } catch(Exception e){
              e.printStackTrace();
          }
          isDiscovered[currentLocation.first][currentLocation.second+1] = true;
        }
      }
    }

    System.out.println("Finished");

  }

}